<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-01-31 19:37:50
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-26 10:17:14
 * ------------------------------------------------ *
 * 商城配置模型                                     *
 * ------------------------------------------------ */ 

namespace Admin\Model;
//use \Think\Model;

class ConfigsModel extends CommonModel {
	/* 获取商城配置信息
	 *---------------------*/
	public function getConfigs(){
		$m= new \Think\Cache\Driver\File();
		$configs = $m->get('config');
		if(!$configs){
			$data = M('configs')->field('fieldCode,fieldValue')->order('parentId asc,fieldSort asc')->select();
			$configs = array();
			if(count($data)>0){
				foreach ($data as $key=>$val){
					
					if($val['fieldCode']=="keywordsSearch"){
						$fieldValue = str_replace("，",",",$val['fieldValue']);
						$configs[$val['fieldCode']] = explode(",",$fieldValue);
					}else{
						$configs[$val['fieldCode']] = $val['fieldValue'];
					}
				}
			}
			unset($data);
			$m->set('config',$configs);
		}
		return $configs;
	}

	/* 获取商城配置表
	 *---------------------*/
	public function getConfigsTable(){
		$data = M('configs')->where('fieldType!="hidden"')->order('parentId asc,fieldSort asc')->select();
		$configs = array();
		if(count($data)>0){
			foreach ($data as $key=>$val){
				if($val['fieldType']=='radio' || $val['fieldType']=='select'){
					$val['txt'] = explode('||',$val['valueRangeTxt']);
					$val['val'] = explode(',',$val['valueRange']);
				}
				$configs[$val['parentId']][] = $val;
			}
		}
		unset($data);
		return $configs;
	}

	/* 获取邮件配置表
	 *---------------------*/
	public function getEmailTable(){
		$data = M('configs')->where('fieldType!="hidden" and parentId=1')->order('parentId asc,fieldSort asc')->select();
		$configs = array();
		if(count($data)>0){
			foreach ($data as $key=>$val){
				if($val['fieldType']=='radio' || $val['fieldType']=='select'){
					$val['txt'] = explode('||',$val['valueRangeTxt']);
					$val['val'] = explode(',',$val['valueRange']);
				}
				$configs[$val['parentId']][] = $val;
			}
		}
		unset($data);
		return $configs;
	}

	/* 保存商城配置信息
	 *-----------------------*/
	public function saveConfigs(){
		$c=M('configs');
		$rs=$c->where('fieldType!="hidden" and parentId='.(int)I('parentId'))->order('parentId asc,fieldSort asc')->select();
		if(empty($rs)){
			return false;
		}
		foreach ($rs as $key => $val){
			if($val['fieldType']=="file"){
				$oldPhoto=$val['fieldValue'];
				if(!empty($_FILES[$val['fieldCode']]['tmp_name'])){
					$$val['fieldCode']=$this->upAvatar($val['fieldCode']);
					if($$val['fieldCode']==false){
						return false;
					}
					$result = $c-> where('fieldCode="'.$val['fieldCode'].'"')->setField('fieldValue',$$val['fieldCode']);
					if(false === $result){
						$this->delAvatar($file);
					    return false;
					}
					if($oldPhoto!="avatar/avatar.jpg"){
						$this->delAvatar($oldPhoto);
					}
				}				
			}else{
				$result = $c-> where('fieldCode="'.$val['fieldCode'].'"')->setField('fieldValue',I($val['fieldCode']));
				if(false === $result){
				    return false;
				}
			}
		}
		$m= new \Think\Cache\Driver\File();
		$m->set('config',null);
		return true;
	}
}